package com.example.forum.auto.tests;

import com.example.forum.auto.common.BrowserName;
import com.example.forum.auto.common.Utils;
import lombok.SneakyThrows;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.io.IOException;
import java.util.List;

/**
 * @author hanzishuai
 * Blog :  https://blog.csdn.net/qrwitu142857
 * @date 2025/5/15 17:46
 * @Description
 */
public class ArticleListPage extends Utils {
    public static final String url = "http://82.157.200.90:58081/index.html";

    public ArticleListPage(BrowserName browserName) {
        super(url, browserName);
    }

    public ArticleListPage() {
        super(url);
    }

    @SneakyThrows
    public void checkPageRight() {
        driver.findElement(By.cssSelector("#article_list_board_title"));
        driver.findElement(By.cssSelector("#search-input"));
        driver.findElement(By.cssSelector("#hzs-forum-content"));
    }

    public void changeBoard() {
        List<WebElement> elements = driver.findElements(By.cssSelector("#topBoardList span.nav-link-title"));
        for (WebElement element : elements) {
            element.click();
        }
        elements.get(0).click();
    }

    public void intoArticleDetail() throws IOException {
        try {
            WebElement element = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong"));
            String text = element.getText();
            wait.until(ExpectedConditions.elementToBeClickable(element));
            Thread.sleep(500);
            element.click();
            wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#details_article_title")));
            assert driver.findElement(By.cssSelector("#details_article_title")).getText().equals(text);
        } catch (Exception e) {
            screenShot("intoArticleDetailError");

        }
    }


    public void intoMyArticlePage() {

        WebElement element = driver.findElement(By.cssSelector("body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div.nav-item.dropdown > a"));
        wait.until(ExpectedConditions.elementToBeClickable(element));
        sleep();
        element.click();
        WebElement element1 = driver.findElement(By.cssSelector("#index_user_settings"));
        sleep();
        wait.until(ExpectedConditions.elementToBeClickable(element1));
        element1.click();
    }

}
